<template>
  <div class="saveAddress">
    <my-header>
      <template #left>
        <van-icon name="arrow-left" @click="$router.back()" />
      </template>
      <template #center> 新增地址 </template>
    </my-header>
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      delete-button-text="取消"
      @save="save"
      @delete="$router.back()"
    />
  </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { areaList } from "@vant/area-data";
import myHeader from "../../components/myHeader.vue";
import { addAddress } from '../../api/secendPage/index';
import { Notify } from "vant";
import { useRouter } from 'vue-router';
export default {
  components: { myHeader },
  setup() {
    const { push } = useRouter();
    const searchResult = ref([
      {
        name: "手机产业园",
        address: "航空港区",
      },
      {
        name: "新亚产业园",
        address: "经开区",
      },
    ]);
    const save = (val) => {
       console.log(val);
       let obj = {
         token: localStorage.getItem('token'),
         address: val.addressDetail,
         cityId: val.areaCode.slice(0,4)+'00',
         code: val.postalCode,
         linkMan: val.name,
         mobile: val.tel,
         provinceId: val.areaCode.slice(0,2)+'0000',
         isDefault: val.isDefault
       }
       addAddress(obj).then(res => {
         if(res.code == 0){
             Notify({ type: "success", message: "添加成功！" });
             push('/site');
         }
       })
    }
    return {
      areaList,
      searchResult,
      save
    };
  },
};
</script>
<style lang="scss" scoped>
.saveAddress {
  height: 100%;
}
</style>